package com.mashibing.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RedisConfig {

    String redisHostPrefix = "redis://";

    @Value("${spring.redis.host:127.0.0.1}")
    private String redisHost;

    @Value("${spring.redis.port:6379}")
    private String redisPort;

    @Value("${spring.redis.port1:6379}")
    private String redisPort1;

    @Value("${spring.redis.port2:6380}")
    private String redisPort2;

    @Value("${spring.redis.port3:6381}")
    private String redisPort3;

    @Value("${spring.redis.port4:6382}")
    private String redisPort4;

    @Value("${spring.redis.port5:6383}")
    private String redisPort5;

    @Bean("redissonBootYml")
    public RedissonClient redissonClient() {
        Config config = new Config();

        config.useSingleServer().setAddress(redisHostPrefix + redisHost + ":" + redisPort).setDatabase(0);

        return Redisson.create(config);

    }

    @Bean("redissonYmlClient")
    public RedissonClient redissonYmlClient() {
//        Config config;
//        try {
//            config = Config.fromYAML(new ClassPathResource("/redisson-config/single-server.yml").getInputStream());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();
    }

    @Bean("redissonMasterSlaveYmlClient")
    public RedissonClient redissonMasterSlaveYmlClient() {
//        Config config;
//        try {
//            config = Config.fromYAML(new ClassPathResource("/redisson-config/master-slave-server.yml").getInputStream());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();
    }

    @Bean("redissonSentinelYmlClient")
    public RedissonClient redissonSentinelYmlClient() {
//        Config config;
//        try {
//            config = Config.fromYAML(new ClassPathResource("/redisson-config/sentinel.yml").getInputStream());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();
    }

    @Bean("redissonClusterYmlClient")
    public RedissonClient redissonClusterYmlClient() {
//        Config config;
//        try {
//            config = Config.fromYAML(new ClassPathResource("/redisson-config/sentinel.yml").getInputStream());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();
    }

    @Bean("redissonRedLockClient1")
    public RedissonClient redissonRedLockClient1() {
//        Config config = new Config();
//
//        config.useSingleServer().setAddress(redisHostPrefix + redisHost + ":" + redisPort1).setDatabase(0);
//
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();

    }

    @Bean("redissonRedLockClient2")
    public RedissonClient redissonRedLockClient2() {
//        Config config = new Config();
//
//        config.useSingleServer().setAddress(redisHostPrefix + redisHost + ":" + redisPort2).setDatabase(0);
//
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();

    }

    @Bean("redissonRedLockClient3")
    public RedissonClient redissonRedLockClient3() {
//        Config config = new Config();
//
//        config.useSingleServer().setAddress(redisHostPrefix + redisHost + ":" + redisPort3).setDatabase(0);
//
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();

    }

    @Bean("redissonRedLockClient4")
    public RedissonClient redissonRedLockClient4() {
//        Config config = new Config();
//
//        config.useSingleServer().setAddress(redisHostPrefix + redisHost + ":" + redisPort4).setDatabase(0);
//
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();

    }

    @Bean("redissonRedLockClient5")
    public RedissonClient redissonRedLockClient5() {
//        Config config = new Config();
//
//        config.useSingleServer().setAddress(redisHostPrefix + redisHost + ":" + redisPort5).setDatabase(0);
//
//        return Redisson.create(config);

        // 测试用例
        return Redisson.create();

    }
}
